package LeetCode;

import Constructs.TreeNode;

import java.util.Arrays;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/6/3.
//
//https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/
public class 将有序数组转换为平衡搜索二叉树 {
    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7,8,9,10};
        TreeNode root = sortedArrayToBST(nums);
        System.out.println(root);
    }
    public static TreeNode sortedArrayToBST(int[] nums) {
        if(nums.length == 0){
            return null;
        }
        TreeNode root = new TreeNode(nums[nums.length /2]);
        root.left = sortedArrayToBST(Arrays.copyOfRange(nums,0,nums.length /2));
        root.right = sortedArrayToBST(Arrays.copyOfRange(nums,nums.length / 2 + 1,nums.length));
        return root;
    }
}
